package com.tanhua.server.controller;

import com.tanhua.server.service.TestSoulService;

import com.tanhua.server.vo.ConclusionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.tanhua.server.vo.PaperListVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import com.tanhua.common.pojo.Answers;

import com.tanhua.common.utils.UserThreadLocal;

import org.apache.commons.lang3.StringUtils;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Collection;
import java.util.Map;


@RestController
@RequestMapping("testSoul")
@Slf4j
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * @MethodName: getReport
     * @Description: 测灵魂-查看结果
     * @Author liuwei
     * @Param
     * @PReturn
     * @Date 2021/4/28
     * @Time 10:49
     */

    @GetMapping("/report/{id}")
    public ResponseEntity<ConclusionVo> getReport(@PathVariable("id") String id) {
        //调业务层
        try {
            ConclusionVo conclusionVo = testSoulService.getReport(id);
            if (null != conclusionVo) {
                return ResponseEntity.ok(conclusionVo);
            }
        } catch (Exception e) {
            log.error("获取测试结果失败......" + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
     /* 提交问卷
     * @param map
     * @return
     */
    @PostMapping
        public ResponseEntity<String> submitTestPaper(@RequestBody Map<String, List<Answers>> map){

        try {
            //返回报告ID
            Collection<List<Answers>> values = map.values();

            for (List<Answers> value : values) {
                System.out.println(value);
        }
            String reportId=this.testSoulService.submitTestPaper(map);
            //如果不为空就成功
            if(StringUtils.isNotEmpty(reportId)){
               return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            log.error("提交问卷失败~ userId = " + UserThreadLocal.get().getId(), e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping
    public ResponseEntity<List<PaperListVo>> paperList() {
        try {
            List<PaperListVo> list = this.testSoulService.queryPaperList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}

